
// 判断是否奇数
export const isOdd = (num: number) => Math.abs(num) % 2 === 1

// 判断是否偶数
export const isEven = (num: number) => Math.abs(num) % 2 === 0

// 向下保留多少位小数
export const floor = (num: number, decimal?: number) => {
  if (decimal === undefined || decimal === null) return num

  if (decimal < 0 || !Number.isInteger(decimal)) throw new Error(`arguments[1] required a positive integer but got ${decimal}`)

  return Math.floor(num * Math.pow(10, decimal)) / Math.pow(10, decimal)
}

// 向上保留多少位小数
export const ceil = (num: number, decimal?: number) => {
  if (decimal === undefined || decimal === null) return num

  if (decimal < 0 || !Number.isInteger(decimal)) throw new Error(`[Error] arguments[1] required a positive integer but got ${decimal}`)

  return Math.ceil(num * Math.pow(10, decimal)) / Math.pow(10, decimal)
}

// 格式化数字
export const numberFormatter = (num: number) => {
  if (num === undefined || num === null) return ''

  return num.toLocaleString()
}